﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bdcs.Application.Mappers.Dto;
using Bdcs.Application.Services.IServices;
using Bdcs.Core.Entity;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Mapster;

namespace Bdcs.Application.Services
{
    public class DeviceService : IDeviceService, ITransient
    {
        private readonly IRepository<CICarInfoMod> _carInfoRepository;

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="carInfoRepository"></param>
        public DeviceService(IRepository<CICarInfoMod> carInfoRepository)
        {
            _carInfoRepository = carInfoRepository;
        }

        /// <summary>
        /// 车辆信息查询分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<PagedList<CarInfoDto>> FindCarInfoPage(int pageIndex = 1, int pageSize = 10)
        {
            var pageResult = await _carInfoRepository
                .DetachedEntities
                .Where(item => item.Model == "私家车")
                .Select(item => new CICarInfoMod
                {
                    No = item.No,
                    CarPlateNo = item.CarPlateNo,
                    CarModel = item.CarModel,
                    Model = item.Model
                })
                .ToPagedListAsync(pageIndex, pageSize);


            return pageResult.Adapt<PagedList<CarInfoDto>>();
        }
    }
}
